package com.wings4.security

import com.wings4.security.utils.EncryptionUtils

class User {

    Integer id;
    String userCode;
    String password;
    Boolean active;

    static mapping = {
        table("security_user")
        discriminator(column: [name: "user_type", length: 255], value: "admin")
    }

    static constraints = {
        userCode(blank:false, size:3..100,unique:true)
        password(blank:false, size:4..100)
    }

    static hasMany = [authorities : Authority, userGroups : UserGroup]



    public static void initialize(){
        EncryptionUtils encUtil = new EncryptionUtils("wings4");
        if(User.getCount() == 0){

            def defaultFeature1 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "userList"
            )

            def defaultFeature2 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "featureList"
            )

            def defaultFeature3 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "authorityList"
            )

            def defaultFeature4 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "createUser"
            )

            def defaultFeature5 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "saveUser"
            )

            def defaultFeature6 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "deleteUser"
            )

            def defaultFeature7 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "createAuthority"
            )

            def defaultFeature8 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "saveAuthority"
            )

            def defaultFeature9 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "deleteAuthority"
            )
            //feature
            def defaultFeature10 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "createFeature"
            )

            def defaultFeature11 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "saveFeature"
            )

            def defaultFeature12 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "deleteFeature"
            )
            def defaultFeature13 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "editUser"
            )
            def defaultFeature14 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "updateUser"
            )
            def defaultFeature15 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "editAuthority"
            )
            def defaultFeature16 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "updateAuthority"
            )
            def defaultFeature17 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "editFeature"
            )
            def defaultFeature18 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "updateFeature"
            )
            def defaultFeature19 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "userGroupList"
            )
            def defaultFeature20 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "createUserGroup"
            )
            def defaultFeature21 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "saveUserGroup"
            )
            def defaultFeature22 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "editUserGroup"
            )
            def defaultFeature23 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "updateUserGroup"
            )
            def defaultFeature24 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "authorityAssignment"
            )
            def defaultFeature25 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "featureAssignment"
            )

            def defaultFeature26 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "userGroupAssignment"
            )
            def defaultFeature27 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "security",
                    operation: "deleteUserGroup"
            )
            def defaultAuthority = new Authority(
                    roleTitle: "gen_admin",
                    description: "general admin"
            )
            def academicConfigurationList = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "academicConfiguration",
                    operation: "academicYearJsonData"
            )
            def academicConfigurationList1 = new Feature(
                    description: "all",
                    documents: "all",
                    fields: "all",
                    module: "academicConfiguration",
                    operation: "academicYearList"
            )
//            def insertClientFeaturesSQL=[
//                    ["Feature to access client List","all","all","client","clientList"] ,
//                    ["Feature to Save client Picture","all","all","client","saveClientPicture"] ,
//                    ["Feature to Save Nominee Picture","all","all","client","saveNomineePicture"] ,
//                    ["Feature to Create client ","all","all","client","createClient"] ,
//                    ["Feature to save Client","all","all","client","saveClient"] ,
//                    ["Feature to save Nominee","all","all","client","saveNominee"] ,
//                    ["Feature to show Client Details","all","all","client","showClientDetails"] ,
//                    ["Feature to update Identification","all","all","client","updateIdentification"],
//                    ["Feature to update Nominee Info","all","all","client","updateNomineeInfo"],
//                    ["Feature to update Nominee Address","all","all","client","updateNomineeAddress"],
//                    ["Feature to delete Client","all","all","client","deleteClient"],
//                    ["Feature to show Client","all","all","client","showClient"],
//                    ["Feature to update Client Name","all","all","client","updateClientName"],
//                    ["Feature to update Contact Info","all","all","client","updateContactInfo"],
//                    ["Feature to update Permanent Address","all","all","client","updatePermanentAddress"],
//                    ["Feature to update Present Address","all","all","client","updatePresentAddress"],
//                    ["Feature to update Others Info","all","all","client","updateOthersInfo"],
//                    ["Feature to save Client Permanent Address Info","all","all","client","saveClientPermanentAddressInfo"],
//                    ["Feature to save Client Present Address Info","all","all","client","saveClientPresentAddressInfo"],
//                    ["Feature to save General Info","all","all","project","saveGeneralInfo"],
//                    ["Feature to create Project","all","all","project","createProject"],
//                    ["Feature to project List","all","all","project","projectList"],
//                    ["Feature to show Project Details","all","all","project","showProjectDetails"],
//                    ["Feature to delete Project","all","all","project","deleteProject"],
//                    ["Feature to save Project Description","all","all","project","saveProjectDescription"],
//                    ["Feature to update Project Description","all","all","project","updateProjectDescription"],
//                    ["Feature to save Project Price","all","all","project","saveProjectPrice"],
//                    ["Feature to save Project Item","all","all","project","saveProjectItem"],
//                    ["Feature to update Details Info","all","all","project","updateDetailsInfo"],
//                    ["Feature to update Project Address","all","all","project","updateProjectAddress"],
//                    ["Feature to update Project Price Record","all","all","project","updateProjectPriceRecord"],
//                    ["Feature to update Project Item Record","all","all","project","updateProjectItemRecord"],
//                    ["Feature to delete Project Item","all","all","project","deleteProjectItem"],
//                    ["Feature to delete Project Price","all","all","project","deleteProjectPrice"],
//                    ["Feature to save Employee Avatar","all","all","employee","saveEmployeeAvatar"],
//                    ["Feature to edit Employee Avatar","all","all","employee","editEmployeeAvatar"]
//            ];
//            def defaultClient=[]
//            insertClientFeaturesSQL?.each {
//                defaultClient.add(new Feature(description: it[0],documents: it[1],fields: it[2], module: it[3],operation: it[4]))
//            }

            //defaultAuthority.save(flush: true)
            //add features to authority
//            defaultClient?.each {
//                defaultAuthority.addToFeatures(it);
//            }
            defaultAuthority.addToFeatures(defaultFeature1).addToFeatures(defaultFeature2)
                    .addToFeatures(defaultFeature3).addToFeatures(defaultFeature4)
                    .addToFeatures(defaultFeature5).addToFeatures(defaultFeature6).addToFeatures(defaultFeature7)
                    .addToFeatures(defaultFeature8).addToFeatures(defaultFeature9).addToFeatures(defaultFeature10)
                    .addToFeatures(defaultFeature11).addToFeatures(defaultFeature12).addToFeatures(defaultFeature13)
                    .addToFeatures(defaultFeature14).addToFeatures(defaultFeature15).addToFeatures(defaultFeature16)
                    .addToFeatures(defaultFeature17).addToFeatures(defaultFeature18).addToFeatures(defaultFeature19)
                    .addToFeatures(defaultFeature20).addToFeatures(defaultFeature21)
                    .addToFeatures(defaultFeature22).addToFeatures(defaultFeature23).addToFeatures(defaultFeature24)
                    .addToFeatures(defaultFeature25).addToFeatures(defaultFeature26).addToFeatures(defaultFeature27).addToFeatures(academicConfigurationList)
                    .addToFeatures(academicConfigurationList1)


            //////    ///// for authority and feature end

            def defaultUser = new User(
                    userCode: "admin",
                    password: encUtil.encrypt("asadmin"),
                    active: true
            )

            defaultUser.save(flush: true);
            defaultUser.addToAuthorities(defaultAuthority).save(flush: true);

        }
    }
}
